// Global Functions

function addLoadEvent(func) {
  var old = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      old();
      func();
    }
  }
}


function insertAfter(newElement, targetElement) {
  var elem = targetElement.parentNode;
  if (elem.lastChild == targetElement) {
    elem.appendChild(newElement);
  } else {
    elem.insertBefore(newElement, targetElement.nextSibling);
  }
}


function addClass(element, value) {
  if (!element.className) {
    element.className = value;
  } else {
    var newClassName = element.className;
    newClassName += " ";
    newClassName += value;
    element.className = newClassName;
  }
}


function highlightPage() {
  var headers = document.getElementsByTagName("header");
  var navs = headers[0].getElementsByTagName("nav");
  var links = navs[0].getElementsByTagName("a");

  for (var i = 0; i < links.length; i++) {
    var linkurl = links[i].getAttribute("href");
    if (window.location.href.indexOf(linkurl) != -1) {
      links[i].className = "here";
      var linktext = links[i].lastChild.nodeValue.toLowerCase();
      document.body.setAttribute("id", linktext);
    }
  }
}


addLoadEvent(highlightPage);
